<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评论分析 - 舆情分析系统</title>
    <link rel="stylesheet" href="../../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../../css/common.css">
    <style>
        .container {
            padding: 20px;
            background: #f5f5f5;
            min-height: 100vh;
        }
        
        .page-header {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .page-title {
            font-size: 24px;
            font-weight: bold;
            color: #333;
            margin: 0;
        }
        
        .filter-section {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .chart-container {
            background: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .chart-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            margin-bottom: 20px;
        }
        
        .chart-wrapper {
            height: 400px;
        }
        
        .stats-cards {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .stat-card {
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        .stat-number {
            font-size: 28px;
            font-weight: bold;
            color: #1890ff;
            margin-bottom: 10px;
        }
        
        .stat-label {
            color: #666;
            font-size: 14px;
        }
        
        .comment-list {
            background: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .comment-item {
            border-bottom: 1px solid #f0f0f0;
            padding: 15px 0;
        }
        
        .comment-item:last-child {
            border-bottom: none;
        }
        
        .comment-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }
        
        .comment-user {
            font-weight: bold;
            color: #333;
        }
        
        .comment-time {
            color: #999;
            font-size: 12px;
        }
        
        .comment-content {
            color: #333;
            line-height: 1.6;
            margin-bottom: 10px;
        }
        
        .comment-meta {
            display: flex;
            gap: 15px;
            font-size: 12px;
            color: #666;
        }
        
        .sentiment-badge {
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 12px;
            color: white;
        }
        
        .sentiment-positive {
            background: #52c41a;
        }
        
        .sentiment-negative {
            background: #ff4d4f;
        }
        
        .sentiment-neutral {
            background: #faad14;
        }
        
        .loading {
            text-align: center;
            padding: 50px;
            color: #999;
        }
    </style>
</head>
<body>
    <div id="app" class="container">
        <!-- 页面头部 -->
        <div class="page-header">
            <h1 class="page-title">评论分析</h1>
        </div>

        <!-- 筛选条件 -->
        <div class="filter-section">
            <form class="layui-form" lay-filter="filterForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">评论时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="dateRange" placeholder="请选择日期范围">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">平台</label>
                        <div class="layui-input-inline">
                            <select name="platform" lay-filter="platform">
                                <option value="">全部平台</option>
                                <option value="weibo">微博</option>
                                <option value="zhihu">知乎</option>
                                <option value="douyin">抖音</option>
                                <option value="bilibili">B站</option>
                                <option value="toutiao">头条</option>
                                <option value="douban">豆瓣</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">情感</label>
                        <div class="layui-input-inline">
                            <select name="sentiment" lay-filter="sentiment">
                                <option value="">全部情感</option>
                                <option value="positive">正面</option>
                                <option value="negative">负面</option>
                                <option value="neutral">中性</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">关键词</label>
                        <div class="layui-input-inline">
                            <input type="text" name="keyword" placeholder="请输入关键词" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button type="button" class="layui-btn" @click="searchData">查询</button>
                        <button type="button" class="layui-btn layui-btn-primary" @click="resetFilter">重置</button>
                    </div>
                </div>
            </form>
        </div>

        <!-- 统计卡片 -->
        <div class="stats-cards">
            <div class="stat-card">
                <div class="stat-number">{{ stats.totalComments }}</div>
                <div class="stat-label">总评论数</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.avgSentiment }}</div>
                <div class="stat-label">平均情感得分</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.avgLength }}</div>
                <div class="stat-label">平均评论长度</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.activeUsers }}</div>
                <div class="stat-label">活跃用户数</div>
            </div>
        </div>

        <!-- 评论情感分布 -->
        <div class="chart-container">
            <h3 class="chart-title">评论情感分布</h3>
            <div id="sentimentChart" class="chart-wrapper"></div>
        </div>

        <!-- 平台评论分布 -->
        <div class="chart-container">
            <h3 class="chart-title">平台评论分布</h3>
            <div id="platformChart" class="chart-wrapper"></div>
        </div>

        <!-- 评论长度分布 -->
        <div class="chart-container">
            <h3 class="chart-title">评论长度分布</h3>
            <div id="lengthChart" class="chart-wrapper"></div>
        </div>

        <!-- 评论时间分布 -->
        <div class="chart-container">
            <h3 class="chart-title">评论时间分布</h3>
            <div id="timeChart" class="chart-wrapper"></div>
        </div>

        <!-- 热门评论列表 -->
        <div class="comment-list">
            <h3 class="chart-title">热门评论列表</h3>
            <div v-if="loading" class="loading">加载中...</div>
            <div v-else>
                <div class="comment-item" v-for="comment in comments" :key="comment.id">
                    <div class="comment-header">
                        <div class="comment-user">{{ comment.username }}</div>
                        <div class="comment-time">{{ comment.commentTime }}</div>
                    </div>
                    <div class="comment-content">{{ comment.content }}</div>
                    <div class="comment-meta">
                        <span>平台: {{ comment.platform }}</span>
                        <span>点赞: {{ comment.likeCount }}</span>
                        <span>回复: {{ comment.replyCount }}</span>
                        <span class="sentiment-badge" :class="getSentimentClass(comment.sentiment)">
                            {{ getSentimentText(comment.sentiment) }}
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入依赖 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="../../lib/echarts/echarts.min.js"></script>
    <script src="../../lib/layui/layui.js"></script>
    <script src="../../js/api.js"></script>
    
    <script>
        new Vue({
            el: '#app',
            data: {
                stats: {
                    totalComments: 0,
                    avgSentiment: 0,
                    avgLength: 0,
                    activeUsers: 0
                },
                comments: [],
                sentimentChart: null,
                platformChart: null,
                lengthChart: null,
                timeChart: null,
                filterParams: {
                    startDate: '',
                    endDate: '',
                    platform: '',
                    sentiment: '',
                    keyword: ''
                },
                loading: false
            },
            mounted() {
                this.initLayui();
                this.initCharts();
                this.loadData();
            },
            methods: {
                // 初始化Layui
                initLayui() {
                    layui.use(['form', 'laydate'], () => {
                        const form = layui.form;
                        const laydate = layui.laydate;
                        
                        // 初始化日期选择器
                        laydate.render({
                            elem: '#dateRange',
                            type: 'date',
                            range: true,
                            done: (value) => {
                                if (value) {
                                    const dates = value.split(' - ');
                                    this.filterParams.startDate = dates[0];
                                    this.filterParams.endDate = dates[1];
                                }
                            }
                        });
                        
                        form.render();
                    });
                },

                // 初始化图表
                initCharts() {
                    this.sentimentChart = echarts.init(document.getElementById('sentimentChart'));
                    this.platformChart = echarts.init(document.getElementById('platformChart'));
                    this.lengthChart = echarts.init(document.getElementById('lengthChart'));
                    this.timeChart = echarts.init(document.getElementById('timeChart'));
                },

                // 加载数据
                async loadData() {
                    try {
                        this.loading = true;
                        
                        // 加载统计数据
                        await this.loadStats();
                        
                        // 加载评论列表
                        await this.loadComments();
                        
                        // 加载图表数据
                        await this.loadChartData();
                        
                    } catch (error) {
                        console.error('加载数据失败:', error);
                        layui.use('layer', () => {
                            layui.layer.msg('加载数据失败', {icon: 2});
                        });
                    } finally {
                        this.loading = false;
                    }
                },

                // 加载统计数据
                async loadStats() {
                    try {
                        const response = await API.opinion.getCommentStats(this.filterParams);
                        this.stats = response;
                    } catch (error) {
                        console.error('加载统计数据失败:', error);
                        // 使用模拟数据
                        this.stats = {
                            totalComments: 8560,
                            avgSentiment: 0.38,
                            avgLength: 45,
                            activeUsers: 3200
                        };
                    }
                },

                // 加载评论列表
                async loadComments() {
                    try {
                        const response = await API.opinion.getComments(this.filterParams);
                        this.comments = response;
                    } catch (error) {
                        console.error('加载评论列表失败:', error);
                        // 使用模拟数据
                        this.comments = this.getMockComments();
                    }
                },

                // 加载图表数据
                async loadChartData() {
                    try {
                        const response = await API.opinion.getCommentCharts(this.filterParams);
                        this.updateCharts(response);
                    } catch (error) {
                        console.error('加载图表数据失败:', error);
                        // 使用模拟数据
                        this.updateCharts(this.getMockChartData());
                    }
                },

                // 更新图表
                updateCharts(data) {
                    this.updateSentimentChart(data);
                    this.updatePlatformChart(data);
                    this.updateLengthChart(data);
                    this.updateTimeChart(data);
                },

                // 更新情感分布图表
                updateSentimentChart(data) {
                    const positive = data.sentimentDistribution?.positive || 40;
                    const neutral = data.sentimentDistribution?.neutral || 45;
                    const negative = data.sentimentDistribution?.negative || 15;
                    
                    const option = {
                        tooltip: {
                            trigger: 'item',
                            formatter: '{a} <br/>{b}: {c} ({d}%)'
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left'
                        },
                        series: [
                            {
                                name: '情感分布',
                                type: 'pie',
                                radius: ['40%', '70%'],
                                avoidLabelOverlap: false,
                                label: {
                                    show: false,
                                    position: 'center'
                                },
                                emphasis: {
                                    label: {
                                        show: true,
                                        fontSize: '18',
                                        fontWeight: 'bold'
                                    }
                                },
                                labelLine: {
                                    show: false
                                },
                                data: [
                                    { value: positive, name: '正面', itemStyle: { color: '#52c41a' } },
                                    { value: neutral, name: '中性', itemStyle: { color: '#faad14' } },
                                    { value: negative, name: '负面', itemStyle: { color: '#ff4d4f' } }
                                ]
                            }
                        ]
                    };
                    this.sentimentChart.setOption(option);
                },

                // 更新平台分布图表
                updatePlatformChart(data) {
                    const platforms = data.platformDistribution?.platforms || ['微博', '知乎', '抖音', 'B站', '头条', '豆瓣'];
                    const counts = data.platformDistribution?.counts || [2100, 1800, 1500, 1200, 900, 1060];
                    
                    const option = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'shadow'
                            }
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'category',
                            data: platforms
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: '评论数',
                                type: 'bar',
                                data: counts
                            }
                        ]
                    };
                    this.platformChart.setOption(option);
                },

                // 更新长度分布图表
                updateLengthChart(data) {
                    const lengthRanges = ['0-20字', '21-50字', '51-100字', '101-200字', '200字+'];
                    const counts = data.lengthDistribution || [1500, 2500, 2000, 1200, 360];
                    
                    const option = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'shadow'
                            }
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'category',
                            data: lengthRanges
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: '评论数',
                                type: 'bar',
                                data: counts
                            }
                        ]
                    };
                    this.lengthChart.setOption(option);
                },

                // 更新时间分布图表
                updateTimeChart(data) {
                    const hours = data.timeDistribution?.hours || ['0', '2', '4', '6', '8', '10', '12', '14', '16', '18', '20', '22'];
                    const counts = data.timeDistribution?.counts || [120, 80, 60, 100, 300, 450, 600, 550, 480, 520, 580, 400];
                    
                    const option = {
                        tooltip: {
                            trigger: 'axis'
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: {
                            type: 'category',
                            data: hours
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: '评论数',
                                type: 'line',
                                smooth: true,
                                data: counts
                            }
                        ]
                    };
                    this.timeChart.setOption(option);
                },

                // 获取模拟评论数据
                getMockComments() {
                    return [
                        {
                            id: 1,
                            username: '科技爱好者',
                            commentTime: '2024-01-15 14:30',
                            content: '这篇文章写得很好，对人工智能的发展趋势分析得很透彻，特别是对未来技术方向的预测很有见地。',
                            platform: '知乎',
                            likeCount: 85,
                            replyCount: 12,
                            sentiment: 'positive'
                        },
                        {
                            id: 2,
                            username: '汽车达人',
                            commentTime: '2024-01-15 13:45',
                            content: '新能源汽车确实是大势所趋，但充电桩建设还需要加快，这是目前最大的痛点。',
                            platform: '微博',
                            likeCount: 120,
                            replyCount: 25,
                            sentiment: 'neutral'
                        },
                        {
                            id: 3,
                            username: '互联网观察者',
                            commentTime: '2024-01-15 12:20',
                            content: '裁员潮确实让人担忧，但这也是行业调整的必然结果，希望企业能够承担更多社会责任。',
                            platform: '头条',
                            likeCount: 95,
                            replyCount: 18,
                            sentiment: 'negative'
                        },
                        {
                            id: 4,
                            username: '教育工作者',
                            commentTime: '2024-01-15 11:15',
                            content: '在线教育确实改变了传统教学模式，但如何保证教学质量是一个需要认真思考的问题。',
                            platform: 'B站',
                            likeCount: 65,
                            replyCount: 8,
                            sentiment: 'neutral'
                        },
                        {
                            id: 5,
                            username: '医疗从业者',
                            commentTime: '2024-01-15 10:30',
                            content: 'AI在医疗领域的应用前景广阔，但数据安全和隐私保护问题不容忽视。',
                            platform: '抖音',
                            likeCount: 78,
                            replyCount: 15,
                            sentiment: 'positive'
                        }
                    ];
                },

                // 获取模拟图表数据
                getMockChartData() {
                    return {
                        sentimentDistribution: {
                            positive: 40,
                            neutral: 45,
                            negative: 15
                        },
                        platformDistribution: {
                            platforms: ['微博', '知乎', '抖音', 'B站', '头条', '豆瓣'],
                            counts: [2100, 1800, 1500, 1200, 900, 1060]
                        },
                        lengthDistribution: [1500, 2500, 2000, 1200, 360],
                        timeDistribution: {
                            hours: ['0', '2', '4', '6', '8', '10', '12', '14', '16', '18', '20', '22'],
                            counts: [120, 80, 60, 100, 300, 450, 600, 550, 480, 520, 580, 400]
                        }
                    };
                },

                // 获取情感样式类
                getSentimentClass(sentiment) {
                    const classes = {
                        'positive': 'sentiment-positive',
                        'negative': 'sentiment-negative',
                        'neutral': 'sentiment-neutral'
                    };
                    return classes[sentiment] || 'sentiment-neutral';
                },

                // 获取情感文本
                getSentimentText(sentiment) {
                    const texts = {
                        'positive': '正面',
                        'negative': '负面',
                        'neutral': '中性'
                    };
                    return texts[sentiment] || '中性';
                },

                // 查询数据
                searchData() {
                    this.loadData();
                },

                // 重置筛选条件
                resetFilter() {
                    this.filterParams = {
                        startDate: '',
                        endDate: '',
                        platform: '',
                        sentiment: '',
                        keyword: ''
                    };
                    layui.use('form', () => {
                        layui.form.val('filterForm', this.filterParams);
                    });
                    this.loadData();
                }
            }
        });

        // 响应式处理
        window.addEventListener('resize', () => {
            const app = document.querySelector('#app').__vue__;
            if (app.sentimentChart) app.sentimentChart.resize();
            if (app.platformChart) app.platformChart.resize();
            if (app.lengthChart) app.lengthChart.resize();
            if (app.timeChart) app.timeChart.resize();
        });
    </script>
</body>
</html>